package com.lanlian.cayman.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lanlian.cayman.R;
import com.lanlian.cayman.activity.RiskStateActivity;
import com.lanlian.cayman.activity.TradeDocumentaryActivity;
import com.lanlian.cayman.adapter.TradeChartsAdapter;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.cayman.view.RiskInvestigationDialog;
import com.lanlian.commonlib.base.BaseLazyFragment;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.RankInfo;
import com.lanlian.commonlib.bean.ReqRankList;
import com.lanlian.commonlib.bean.RespRankList;
import com.lanlian.commonlib.common.SystemConfig;
import com.lanlian.commonlib.event.EventCode;
import com.lanlian.commonlib.event.EventMessage;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.uikit.ActionItem;
import com.lanlian.uikit.CustomBottomSheetDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/14 16:51
 *     desc   : 交易排行榜详情
 *     version: 1.0
 * </pre>
 */
public class TradeChartsDetailFragment extends BaseLazyFragment {

    private SmartRefreshLayout mLayRefresh;
    private RecyclerView mRecycleView;
    private ArrayList<RankInfo> mList = new ArrayList<>();
    private TradeChartsAdapter mAdapter;

    private int mType = 1;
    private int mOrderType;

    public static TradeChartsDetailFragment getInstance(int orderType) {
        TradeChartsDetailFragment fragment = new TradeChartsDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("orderType", orderType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_trade_charts_detail, null);
        initData();
        initView(view);
        setListeners();
        initAdapter();
        return view;
    }

    @Override
    protected void loadData() {
        mLayRefresh.autoRefresh(500);
    }

    @Override
    protected void initData() {
        super.initData();
        mOrderType = getArguments().getInt("orderType");
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mLayRefresh = view.findViewById(R.id.lay_refresh);
        mRecycleView = view.findViewById(R.id.recycle_view);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mLayRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mRecycleView.scrollToPosition(0);
                getRankList();
            }
        });
    }

    private void initAdapter() {
        mAdapter = new TradeChartsAdapter(getActivity(), R.layout.adapter_trade_charts, mList);
        mRecycleView.setAdapter(mAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        mRecycleView.setLayoutManager(linearLayoutManager);
        mAdapter.bindToRecyclerView(mRecycleView);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int pos) {
                CustomBottomSheetDialog dialog = new CustomBottomSheetDialog(getActivity(), "请选择");
                dialog.addAction(new ActionItem("跟单"));
                dialog.setOnItemClickListener(new CustomBottomSheetDialog.OnItemOnClickListener() {
                    @Override
                    public void onItemClick(ActionItem item, int position) {
                        switch (item.mTitle) {
                            case "跟单":
                                if (mDBManager.getUser() != null && mDBManager.getUser().getRiskAuthState() == 0 &&
                                        mDBManager.getIsOldUser() == 0) {
                                    new RiskInvestigationDialog(getActivity(), new RiskInvestigationDialog.OnClickListener() {
                                        @Override
                                        public void onWriteClick() {
                                            startActivity(new Intent(getActivity(), RiskStateActivity.class)
                                                    .putExtra("mobile", mDBManager.getUser().getMobileNo()));
                                        }

                                        @Override
                                        public void onCloseClick() {
                                        }
                                    }).show();
                                    return;
                                }
                                startActivity(new Intent(getActivity(), TradeDocumentaryActivity.class)
                                        .putExtra("no", mList.get(pos).getNo()));
                                break;
                            case "成交记录":
                                break;
                            default:
                                break;
                        }
                    }
                });
                dialog.showDialog();
            }
        });
    }

    private void getRankList() {
        ReqRankList reqRankList = new ReqRankList();
        if (mType != 0) {
            reqRankList.setType(mType);
        }
        reqRankList.setOrderType(mOrderType);
        VolleyRequestUtils.getInstance().sendRequest(this.toString(), reqRankList, new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                mLayRefresh.finishRefresh();
                RespRankList respRankList = (RespRankList) response;
                mList = respRankList.getDataList();
                mAdapter.setNewData(mList);
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                mLayRefresh.finishRefresh();
                showToast(baseResponse.getMsg());
            }
        });
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    public void onReceiveEvent(EventMessage event) {
        super.onReceiveEvent(event);
        switch (event.getCode()) {
            case EventCode.EVENT_RANKLIST_REFRESH:
                mType = Integer.valueOf((String) event.getData());
                mLayRefresh.autoRefresh();
                break;
            case EventCode.EVENT_MARKET_COLOR_CHANGE:
                mAdapter.notifyDataSetChanged();
                break;
            default:
                break;
        }
    }
}
